/*
 //两种调用方式
var template1="我是{0}，今年{1}了";
var template2="我是{name}，今年{age}了";
var result1=template1.format("loogn",22);
var result2=template1.format({name:"loogn",age:22});
//两个结果都是"我是loogn，今年22了" 
 */
String.prototype._format = function(args) {
	if (arguments.length > 0) {
		var result = this;
		if (arguments.length == 1 && typeof (args) == "object") {
			for ( var key in args) {
				var reg = new RegExp("({" + key + "})", "g");
				result = result.replace(reg, args[key]);
			}
		} else {
			for ( var i = 0; i < arguments.length; i++) {
				if (arguments[i] == undefined) {
					return "";
				} else {
					var reg = new RegExp("({[" + i + "]})", "g");
					result = result.replace(reg, arguments[i]);
				}
			}
		}
		return result;
	} else {
		return this;
	}
};

/*
 * 方法:Array.remove(dx) 通过遍历,重构数组 功能:删除数组元素. 参数:dx删除元素的下标.
 */
Array.prototype._remove = function(dx) {
	if (isNaN(dx) || dx > this.length) {
		return false;
	}
	for ( var i = 0, n = 0; i < this.length; i++) {
		if (this[i] != this[dx]) {
			this[n++] = this[i];
		}
	}
	this.length -= 1;
};
requestJson = function (url, data, async, fun) {
	var responseData = {};
	responseData.status = -100;
	responseData.message = '网络不给力!';
	responseData.data = null;

	$.ajax({
		async: async || false,
		type: "POST",
		url: url + "?t=" + jst,
		data: data,
		dataType: "json",
		success: function (data) {
			responseData = data;

			if (fun) {
				fun(data);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown, t1) {
			responseData.status = -101;
			responseData.message = XMLHttpRequest.statusText;

			if (fun) {
				fun(responseData);
			}
		}
	});

	return responseData;
};

// async false 同步 true 异步
returnjson = function(url, data, async, fun) {
	var t = {};
	t.flag=0;
	t.msg="网络不给力哦。。";
	$.ajax({
		async : async || false,
		type : "POST",
		url : url + '?t=' + jst,
		data : data,
		dataType : 'json',
		success : function(msg) {
			if(typeof msg!="object") {
				msg={};
				msg.flag=0;
				msg.msg=msg;
			}

			t = msg;
			if (fun) {
				fun(t);
			}
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {

			msg={};
			msg.flag=0;
			msg.msg="错误("+textStatus+")["+errorThrown+"]:"+XMLHttpRequest.responseText;
			t = msg;
			if (fun) {
				fun(t);
			}
		}
	});
	return t;
};
returnjson_nojst = function(url, data, async, fun) {
	var t = {};
	$.ajax({
		async : async || false,
		type : "POST",
		url : url ,
		data : data,
		dataType : 'json',
		success : function(msg) {
			t = msg;
			if (fun) {
				fun(t);
			}
		}
	});
	return t;
};
returnstring = function(url) {

	var t = "";
	$.ajax({
		async : false,
		type : "GET",
		dataType : "text",
		url : url + '?t=' + jst,
		data : {},
		success : function(msg) {
			t = msg;

		}
	});
	return t;
};
var jst = new Date().getTime();
Date.prototype._format = function(format) // yyyy-MM-dd hh:mm:ss

{

	var o = {

		"M+" : this.getMonth() + 1, // month

		"d+" : this.getDate(), // day

		"h+" : this.getHours(), // hour

		"m+" : this.getMinutes(), // minute

		"s+" : this.getSeconds(), // second

		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter

		"S" : this.getMilliseconds()
	// millisecond

	};

	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1,

		(this.getFullYear() + "").substr(4 - RegExp.$1.length));

	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))

			format = format.replace(RegExp.$1,

			RegExp.$1.length == 1 ? o[k] :

			("00" + o[k]).substr(("" + o[k]).length));

	return format;

};

var jst = new Date().getTime();

String.prototype._trim = function() {
	var str = this;
	return str.replace(/(^\s*)|(\s*$)/g, "");
};
// 分页计算
// 获得总页数
getPageCount_page = function(recordcount, pagesize) {
	recordcount = parseInt(recordcount);
	pagesize = parseInt(pagesize);
	return parseInt((recordcount + pagesize - 1) / pagesize);
};
encodeURI_ext = function(str) {
	return encodeURIComponent(str);
	// return encodeURI(str);
};
// input 只能输入数字
checkNumVal = function(me) {
	var val = me.value;

	if (isNaN(val)) {
		me.value = "0";
	}
	if (val._trim() == "") {
		me.value = "0";
	}
};
// input 只能输入整形
checkNumVal_int = function(me) {

	if(event.keyCode !=37 && event.keyCode != 39){
		if (! /^\d+$/ig.test(me.value)){
			me.value='';
			return;
		}
	}
	//去掉前面的0
	me.value=me.value.replace(/\b(0+)/gi,"");
};

// 获取整形
getNumVal_int = function(val) {



	if (isNaN(val)) {
		return "0";
	}
	if (val._trim() == "") {
		return "0";
	}
	return parseInt(val);
};

// 获取url参数集合字符串
getUriPmStr = function() {
	var v = window.location.href;
	var strs = v.split("?");
	if (strs.length > 1) {
		return strs[1];
	} else {
		return "";
	}
};
//时间戳转时间
timestampToDatetime=function(v){
	if(v==null)
		return "";
	if(v=="")
		return "";
	var date = new Date(v);
	Y = date.getFullYear() + '-';
	M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	D = (date.getDate()<10 ? '0'+date.getDate() : date.getDate() )+ ' ';
	h = (date.getHours()<10 ? '0'+date.getHours() : date.getHours())+ ':';
	m = (date.getMinutes()<10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
	s = date.getSeconds()<10 ? '0'+date.getSeconds():date.getSeconds(); 
	return Y+M+D+h+m+s; 

};
timestampToDatetime_ymd=function(v){
	if(v==null)
		return "";
	if(v=="")
		return "";
	var date = new Date(v);
	Y = date.getFullYear() + '-';
	M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	D = (date.getDate()<10 ? '0'+date.getDate() : date.getDate() )+ ' ';
	h = (date.getHours()<10 ? '0'+date.getHours() : date.getHours())+ ':';
	m = (date.getMinutes()<10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
	s = date.getSeconds()<10 ? '0'+date.getSeconds():date.getSeconds();
	return Y+M+D;

};
//对象合并
mergeJsonObject = function(jsonbject1, jsonbject2)  
{  
    var resultJsonObject={};  
    for(var attr in jsonbject1){  
        resultJsonObject[attr]=jsonbject1[attr];  
    }  
    for(var attr in jsonbject2){  
        resultJsonObject[attr]=jsonbject2[attr];  
    }  

    return resultJsonObject;  
};  
//选择框全选、取消
all_check=function(me,targetName){
	var target="input[name='"+targetName+"']";
	if($(me).prop("checked") == true){ //check all
		   $(target).each(function(){
		    	$(this).prop("checked",true);
		   });
		  }else{
		   $(target).each(function(){
		    	$(this).prop("checked",false);
		   });
		  }
};
//获取url参数
function getQueryString(url,name) {
	
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");	   
    var tmp=url.split("?")[1];
    var r = tmp.match(reg);
    if (r != null) return unescape(r[2]); return null;
}
//校验手机号
function checkMobile(str) {
	var re = /^1\d{10}$/;
	if (re.test(str)) {
		return true;
	} else {
		return false;
	}
}

//微信刷新页面
function reload_weixin_page(){
	var url=window.location.href;
	var sp='?t=';
	if(url.indexOf("?")!=-1)
	{
		sp='&t=';
	}

	window.location.href=window.location.href+sp+jst;
}

//拼接参数
function addUrlPara(url,name, value) {
	var currentUrl = url.split('#')[0];
	if (/\?/g.test(currentUrl)) {
		if (/name=[-\w]{4,25}/g.test(currentUrl)) {
			currentUrl = currentUrl.replace(/name=[-\w]{4,25}/g, name + "=" + value);
		} else {
			currentUrl += "&" + name + "=" + value;
		}
	} else {
		currentUrl += "?" + name + "=" + value;
	}
	if (url.split('#')[1]) {
		url = currentUrl + '#' + window.location.href.split('#')[1];
	} else {
		url = currentUrl;
	}
	return url;
}